﻿using System;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Runtime.InteropServices;
using System.Web;
using LinqKit;
using PNProject.EF;
using PNProject.Core;

namespace PNProject.Core.Converter
{
    public class ItemConverter
    {
        public static Item ToItem(ItemModel item)
        {
            try
            {
                Item obj = new Item();
                obj.ItemID = item.ItemId;
                obj.ItemName = item.ItemName;
                obj.CreatedDate = item.CreatedDate;
                obj.ModifiedDate = item.ModifiedDate;
                obj.IsActive = item.Status;
                obj.IsDelete = item.IsDelete;
                obj.ImagesItems = new EntityCollection<ImagesItem>();
                obj.ItemDetails = new EntityCollection<ItemDetail>();
                foreach (var img in item.Images)
                {
                    var imageItem = new ImagesItem();
                    imageItem.Id = Guid.NewGuid();
                    imageItem.ItemId = obj.ItemID;
                    imageItem.Url = img.Url;
                    imageItem.CreatedDate = item.CreatedDate;
                    imageItem.ModifiedDate = item.ModifiedDate;
                    obj.ImagesItems.Add(imageItem);
                }
              

                var detail = new ItemDetail();
                detail.Id = Guid.NewGuid();
                detail.ItemId = obj.ItemID;
                detail.Description = item.Description;
                detail.CreatedDate = item.CreatedDate;
                detail.Details = item.Details;
                obj.ItemDetails.Add(detail);
                obj.ItemType = item.ItemType;
                detail.Manufacture = item.Manufacture;
                detail.ElectricPressure = item.ElectricPressure;
                detail.Size = item.Size;
                return obj;            
            }
            catch(Exception e)
            {
                return new Item() {};
            }
        }

        public static ItemModel ItemTo(Item item)
        {
            ItemModel obj = new ItemModel();
            obj.ItemId = item.ItemID;
            obj.ItemName = item.ItemName;
            obj.CreatedDate = item.CreatedDate.Value;
            obj.ModifiedDate = item.ModifiedDate.Value;
            obj.IsDelete = item.IsDelete.Value;
            obj.Status = item.IsActive.Value;
            var detail = item.ItemDetails.SingleOrDefault();
            if (detail != null)
            {
                obj.Description = detail.Description;
                obj.Details = detail.Details;
                obj.Manufacture = detail.Manufacture;
                obj.ElectricPressure = detail.ElectricPressure;
                obj.Size = detail.Size;
            }
             obj.Images = new List<ImagesItem>();
            item.ImagesItems.ForEach(a =>
                                     {
                                         var img = new ImagesItem();
                                         img.Url = a.Url;
                                         img.Id = img.Id;
                                         obj.Images.Add(img);
                                     });
            obj.Status = (item.IsActive.HasValue && item.IsActive.Value)?true:false;
            return obj;            
        }
        public static Item CopyFeedback(Item item)
        {
            Item obj = new Item();
            try
            {
                obj.ItemID = item.ItemID;
                obj.ItemName = item.ItemName;
                obj.ItemNameEN = item.ItemNameEN;
                obj.ItemType = item.ItemType;
                obj.Lang = item.Lang;
                obj.IsActive = item.IsActive;
                obj.IsDelete = item.IsDelete;
                obj.CreatedDate = item.CreatedDate;
                obj.ModifiedDate = item.ModifiedDate;
                obj.TLink = item.TLink;
                obj.TLinkEN = item.TLinkEN;
                obj.Category = item.Category;
                obj.ImagesItems = new EntityCollection<ImagesItem>();
                item.ImagesItems.ForEach(a =>
                                         {
                                             var entity = new ImagesItem();
                                             entity.Id = a.Id;
                                             entity.Item = a.Item;
                                             entity.ItemId = a.ItemId;
                                             entity.Language = a.Language;
                                             entity.CreatedDate = a.CreatedDate;
                                             entity.Url = a.Url;
                                             entity.OrderNumber = a.OrderNumber;
                                             entity.ModifiedDate = a.ModifiedDate;
                                             //entity.ItemReference = a.ItemReference;
                                             obj.ImagesItems.Add(entity);
                                         });
                obj.ItemProperties = new EntityCollection<ItemProperty>();
                    item.ItemProperties.ForEach(a =>
                                                {
                                                    var entity = new ItemProperty();
                                                    entity.ItemID = a.ItemID;
                                                    entity.PKey = a.PKey;
                                                    entity.PValue = a.PValue;
                                                    //entity.ItemReference = a.ItemReference;
                                                    obj.ItemProperties.Add(a);
                                                });
                obj.ItemDetails = new EntityCollection<ItemDetail>();
                item.ItemDetails.ForEach(a =>
                                         {
                                             var entity = new ItemDetail();
                                             entity.Id = a.Id;
                                             entity.Description = a.Description;
                                             entity.Details = a.Details;
                                             entity.CreatedDate = a.CreatedDate;
                                             entity.ModifiedDate = a.ModifiedDate;
                                             entity.ItemId = a.ItemId;
                                             entity.Type = a.Type;
                                             obj.ItemDetails.Add(entity);             
                                         });
                return obj;
            }
            catch (Exception e)
            {
                return new Item();
            }
        }
    }
}